package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public Department selectByPrimaryKey(Long id) {
        Department department = departmentMapper.selectByPrimaryKey(id);
        return department;
    }

    @Override
    public List<Department> selectAll() {
        List<Department> departments = departmentMapper.selectAll();
        return departments;
    }

    @Override
    public void updateByPrimaryKey(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        //使用 分页插件,传入当前页,每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //调用startPage 方法后 ,会对下一句sql进行拦截,并进行改造成分页的 sql  千万注意哦
        List<Department> departments = departmentMapper.selectForList(qo);
        return new PageInfo<Department>(departments);
    }
        //两条sql 查出来的 结果
       // int count = departmentMapper.selectForCount(qo);
//        List<Department> departments = departmentMapper.selectForList(qo);
//        if (count == 0) {
//            return new PageResult(qo.getCurrentPage(), qo.getPageSize(), Collections.emptyList(), count);
//        }
//        return new PageResult(qo.getCurrentPage(), qo.getPageSize(), departments, count);
//    }
}
